#!/bin/bash

# 查看服务日志
# View Service Logs

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"

cd "$PROJECT_ROOT"

SERVICE=$1

if [ -z "$SERVICE" ]; then
    echo "📋 查看所有服务日志"
    echo "💡 按 Ctrl+C 退出"
    echo ""
    docker compose -f docker-compose.databases.yml logs -f &
    docker compose -f docker-compose.services.yml logs -f
else
    # 检查服务在哪个 compose 文件中
    if docker compose -f docker-compose.databases.yml ps | grep -q "$SERVICE"; then
        echo "📋 查看数据库服务日志: $SERVICE"
        docker compose -f docker-compose.databases.yml logs -f "$SERVICE"
    elif docker compose -f docker-compose.services.yml ps | grep -q "$SERVICE"; then
        echo "📋 查看应用服务日志: $SERVICE"
        docker compose -f docker-compose.services.yml logs -f "$SERVICE"
    else
        echo "❌ 服务不存在: $SERVICE"
        echo ""
        echo "可用的服务："
        echo "  数据库: postgresql, mongodb, redis"
        echo "  应用: web, api, crawler, ai"
        exit 1
    fi
fi
